// $Id: headers.c 81 2011-04-24 07:37:10Z cedric.shih@gmail.com $
/*
 * Copyright (C) 2010 Cedric Shih <cedric.shih@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <string.h>

#include <nio/buffer.h>

#include "common.h"
#include "headers.h"

#include "niohttp/test.h"

static struct niohttp_headers *headers;

static void test_niohttp_headers_set(struct niohttp_headers *header,
		const char *key, const char *value)
{
	int rc;
	char *k, *v;
	k = strdup(key);
	v = strdup(value);
	rc = niohttp_headers_set(header, k, v);
	free(k);
	free(v);
	TEST_ASSERT_EQUAL_INT(0, rc);
}

static void test_setup(void)
{
	headers = niohttp_headers_new();
}

static void test_teardown(void)
{
	niohttp_headers_free(headers);
}

static void test_init(void)
{
	TEST_ASSERT_NOT_NULL(headers);
}

static void test_data(void)
{
	int rc;

	TEST_ASSERT_NULL(niohttp_headers_get(headers, "foo"));
	TEST_ASSERT_NULL(niohttp_headers_get(headers, "john"));

	test_niohttp_headers_set(headers, "foo", "bar");
	test_niohttp_headers_set(headers, "john", "doh");

	TEST_ASSERT_EQUAL_STRING("bar", niohttp_headers_get(headers, "foo"));
	TEST_ASSERT_EQUAL_STRING("bar", niohttp_headers_get(headers, "foo"));
	TEST_ASSERT_EQUAL_STRING("bar", niohttp_headers_get(headers, "Foo"));
	TEST_ASSERT_EQUAL_STRING("bar", niohttp_headers_get(headers, "FOO"));
	TEST_ASSERT_EQUAL_STRING("doh", niohttp_headers_get(headers, "john"));
	TEST_ASSERT_EQUAL_STRING("doh", niohttp_headers_get(headers, "John"));
	TEST_ASSERT_EQUAL_STRING("doh", niohttp_headers_get(headers, "JOHN"));

	test_niohttp_headers_set(headers, "foo", "pub");

	rc = niohttp_headers_remove(headers, "john");
	TEST_ASSERT_EQUAL_INT(0, rc);

	TEST_ASSERT_EQUAL_STRING("pub", niohttp_headers_get(headers, "foo"));
	TEST_ASSERT_NULL(niohttp_headers_get(headers, "john"));
}

TestRef niohttp_headers_test()
{
	EMB_UNIT_TESTFIXTURES(fixtures) {
		new_TestFixture("niohttp_headers_test_init", test_init),
		new_TestFixture("niohttp_headers_test_data", test_data),
	};
	EMB_UNIT_TESTCALLER(test_case, "niohttp_headers_test", test_setup, test_teardown, fixtures);
	return (TestRef)&test_case;
}
